package Comparator01;

import java.util.Arrays;
import java.util.Comparator;

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}

class NameCompare implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

class AgeCompare implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge()- o2.getAge();
    }
}

public class Test {


    public static void main(String[] args) {
        Student[] students = new Student[3];
        NameCompare nameCompare = new NameCompare();
        AgeCompare ageCompare = new AgeCompare();
        students[0] = new Student("zhangsan",18);
        students[1] = new Student("lisi",39);
        students[2] = new Student("wangwu",26);
        Arrays.sort(students,nameCompare);//按姓名排序
        System.out.println(Arrays.toString(students));
        Arrays.sort(students,ageCompare);//按年龄排序
        System.out.println(Arrays.toString(students));
    }

}